package dfsketches.pathfind;

import dfsketches.bicycles.BicycleQueue;
import dfsketches.bicycles.BicycleStack;

import java.util.Hashtable;

/**
 * A structure to store Paths and their IDs.
 */
public class PathStore {
    private BicycleQueue ids;
    private Hashtable<Integer, BicycleStack> paths;

    public PathStore() {
        ids = new BicycleQueue(0);
        ids.push(ids.size());

    }

    public int addPath(BicycleStack path) {
        if (ids.isEmpty()) ids.push(ids.size());

        int id = ids.pop();
        paths.put(id, path);
        return id;
    }

    public int nextStep(int id) {
        BicycleStack path = paths.get(id);
        if (path == null) {
            return -1;
        } else if (path.isEmpty()) {
            deletePath(id);
            return -1;
        }
        return path.pop();
    }

    public void deletePath(int id) {
        if (paths.containsKey(id)) {
            paths.remove(id);
            ids.push(id);
        }
    }
}
